<?php
class PAPager extends sfDoctrinePager{
  
  public function __construct($class, $max_per_page)
  {
    parent::__construct($class, $max_per_page);
  }
  
  public function getPageForObject($id, $category, $sortBy, $sortOrder)
  {
    $query = Doctrine_Query::create()
            ->select("s.id")
            ->from("StoreItem s")
            ->andWhere("s.category_id = ?", $category->getId())
            ->orderBy("s.{$sortBy[0]} {$sortOrder}, s.{$sortBy[1]}");
            
    $itemsArray = $query->execute()->toArray();
    $index = 0;
    if(is_array($itemsArray)){
      foreach($itemsArray as $key => $item){
        if($item['id'] == $id){
          $index = $key;
          break;
        }
      }
    }
    
    $page = ceil(($index + 1) / $this->getMaxPerPage());
    
    return $page;
  }
}